﻿import numpy as np
import matplotlib.pyplot as plt

# 定义 x 的范围
x = np.linspace(0, 1000, 400)

# 根据方程计算 y
y1 = (0.018 / 0.012) * x - (1.78 / 0.012)
y2 = (220/550)*x
y3 = (220/425)*x

# 仅保留第一象限的部分   
y1_first_quadrant = y1[y1 > 0]   
x1_first_quadrant = x[y1 > 0]
   
y2_first_quadrant = y2[y2 > 0]   
x2_first_quadrant = x[y2 > 0]
   
y3_first_quadrant = y3[y3 > 0]   
x3_first_quadrant = x[y3 > 0]

# 定义数据      
data = {
    'city': ['Beijing', 'Beijing', 'Beijing', 'Beijing', 'Beijing', 
            'Zibo', 'Zibo', 'Zibo', 'Zibo', 'Zibo', 'Zibo'],
    'price': [400.0, 375.0, 890.0, 271.0, 190.0, 
            90.0, 120.0, 57.0, 97.0, 40.0, 113.0],
    'area': [120.0, 110.0, 210.0, 86.0, 47.0, 
            97.0, 130.0, 60.0, 107.0, 50.0, 100.0]      
}
   
# 创建颜色映射      
colors = {'Beijing': 'red', 'Zibo': 'blue'}      
city_colors = [colors[city] for city in data['city']]
   
# 绘制散点图      
# plt.figure(figsize=(10, 6))      
plt.scatter(data['price'], data['area'], c=city_colors)

#决策边界
plt.plot(x3_first_quadrant, y3_first_quadrant, label='Max Variance', color='orange')
plt.plot(x2_first_quadrant, y2_first_quadrant, label='Max Property', color='purple')
plt.plot(x1_first_quadrant, y1_first_quadrant, label='Max Margin', color='green')

# 添加图例      
for city, color in colors.items():
    plt.scatter([], [], c=color, label=city)
   
plt.xlabel('price (wan yuan)')      
plt.ylabel('area (m2)')      
plt.title('The relationship between urban housing prices and area')      
plt.legend()      
plt.grid(True)  
plt.xlim([0, 900])  # 可选：设置x轴范围
plt.ylim([0, 250])  # 可选：设置y轴范围
plt.show()
